﻿using System;
using System.Collections.Generic;


namespace Najak.Editor
{
	public abstract partial class EditorAction
	{
		public void Apply()
		{
			if (!OnApply(Scene.Instance))
				return;

			int undoCount = Scene.Instance.EditorActions.MaxActionQueueIndex - Scene.Instance.EditorActions.ActionQueueIndex;
			if (undoCount > 0)
			{	// trims off 
				Scene.Instance.EditorActions.AppliedActions.RemoveRange(Scene.Instance.EditorActions.ActionQueueIndex + 1, undoCount);
			}
			Scene.Instance.EditorActions.AppliedActions.Add(this);

			while (Scene.Instance.EditorActions.AppliedActions.Count > ActionManager.MaxActions)
			{
				Scene.Instance.EditorActions.AppliedActions.RemoveAt(0);
			}

			Scene.Instance.EditorActions.ActionQueueIndex = Scene.Instance.EditorActions.MaxActionQueueIndex;
			Scene.Instance.EditorActions.IsDirty = true;
		}
		abstract protected bool OnApply(Scene scene);

		protected void Undo(Scene scene)
		{
			OnUndo(scene);
			Scene.Instance.EditorActions.IsDirty = true;
		}
		abstract protected void OnUndo(Scene scene);

		virtual protected void AdjustLocation(int minColumn, int minRow)
		{ }

	}
}
